package com.simple.service.slr;

import com.simple.po.SlrKeepGrant;
import com.xunj.core.CoreDao;

public class SlrKeepGrantService {
	
	private CoreDao dao;

	public SlrKeepGrantService(CoreDao dao) {
		this.dao = dao;
	}

	
	/**
	 * 检测发放工资是否大于存留数
	 * @param filePath
	 * @throws Exception
	 */
	public String checkSlrKeepGrantAmount(SlrKeepGrant slrKeepGrant) throws Exception
	{
		String hql = "select sum(slrKeepGrantAmount) from SlrKeepGrant where " +
			"areaId='"+ slrKeepGrant.getAreaId() + "' and "+
			"areaDeptId='"+ slrKeepGrant.getAreaDeptId() + "' and "+
			"keepGrantType='留存' and state='在用'";
		if(slrKeepGrant.getSalaryKeepGrantId()!=null)
			hql+=" and salaryKeepGrantId<>'"+slrKeepGrant.getSalaryKeepGrantId()+"'";
		if(slrKeepGrant.getTargetId()!=null&&!"".equals(slrKeepGrant.getTargetId()))
			hql+=" and targetId='"+ slrKeepGrant.getTargetId() + "' " ;
		else
			hql+=" and targetName='"+ slrKeepGrant.getTargetName() + "' " ;
				
		Double yl = (Double) dao.findAll(hql).get(0);
		yl=yl==null?0d:yl;
		hql = "select sum(slrKeepGrantAmount) from SlrKeepGrant where " +
			"areaId='"+ slrKeepGrant.getAreaId() + "' and "+
			"areaDeptId='"+ slrKeepGrant.getAreaDeptId() + "' and "+
			"keepGrantType='发放' and state='在用'";
		if(slrKeepGrant.getSalaryKeepGrantId()!=null)
			hql+=" and salaryKeepGrantId<>'"+slrKeepGrant.getSalaryKeepGrantId()+"'";
		if(slrKeepGrant.getTargetId()!=null&&!"".equals(slrKeepGrant.getTargetId()))
			hql+=" and targetId='"+ slrKeepGrant.getTargetId() + "' " ;
		else
			hql+=" and targetName='"+ slrKeepGrant.getTargetName() + "' " ;
		
		Double ff = (Double) dao.findAll(hql).get(0);
		ff=ff==null?0d:ff;
		//添加或修改，如操作数据为发放(留存-发放-本次发放)，删除，操作如为留存(留存-发放-本次预留)
		String data = "ok";
		if(yl-ff-slrKeepGrant.getSlrKeepGrantAmount()<0)
		data = (yl-ff)+"";
		return data;
	}


}
